/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FontChooser.java
 *
 * Created on 09/11/2009, 15:30:40
 */

package com.power.visual;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import javax.swing.JColorChooser;
import javax.swing.JDialog;

/**
 *
 * @author f6036477
 */
public class FontChooser extends javax.swing.JDialog {

    private static String[] fonts =
        GraphicsEnvironment.getLocalGraphicsEnvironment()
        .getAvailableFontFamilyNames();

    private boolean closed = false;

    private Color colorChoosed = Color.BLACK;


    /** Creates new form FontChooser */
    public FontChooser(Component owner) {
        super();
        this.setTitle("Font Chooser");
        initComponents();
        this.setModal(true);
        this.setResizable(false);
        lazyDogLabel.setFont(
            this.getSelectedFont());
        this.setLocationRelativeTo(owner);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    fontsCombo = new javax.swing.JComboBox();
    jLabel2 = new javax.swing.JLabel();
    sizeText = new javax.swing.JTextField();
    cancelButton = new com.power.visual.BlackButton();
    saveButton = new com.power.visual.BlackButton();
    lazyDogLabel = new javax.swing.JLabel();
    colorButton = new com.power.visual.BlackButton();
    jLabel4 = new javax.swing.JLabel();
    colorLabel = new javax.swing.JLabel();

    setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    setTitle("FontChooser");

    jLabel1.setText("Font:");

    fontsCombo.setModel(new javax.swing.DefaultComboBoxModel(fonts));
    fontsCombo.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        fontsComboActionPerformed(evt);
      }
    });

    jLabel2.setText("Size:");

    sizeText.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    sizeText.setText("12");
    sizeText.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        sizeTextActionPerformed(evt);
      }
    });

    cancelButton.setText("Cancel");
    cancelButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        cancelButtonActionPerformed(evt);
      }
    });

    saveButton.setText("Save");
    saveButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        saveButtonActionPerformed(evt);
      }
    });

    lazyDogLabel.setFont(new java.awt.Font("SansSerif", 0, 12)); // NOI18N
    lazyDogLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    lazyDogLabel.setText("The quick brown fox jumps over the lazy dog");
    lazyDogLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(153, 153, 153)));

    colorButton.setText("Color");
    colorButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        colorButtonActionPerformed(evt);
      }
    });

    jLabel4.setText("Choose Font Color:");

    colorLabel.setOpaque(true);
    colorLabel.setBackground(Color.BLACK);
    colorLabel.setText(" ");
    colorLabel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(153, 153, 153), 1, true));

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addGap(28, 28, 28)
            .addComponent(jLabel4)
            .addGap(18, 18, 18)
            .addComponent(colorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(colorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addGroup(layout.createSequentialGroup()
            .addGap(63, 63, 63)
            .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(26, 26, 26)
            .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addGroup(layout.createSequentialGroup()
            .addGap(18, 18, 18)
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(fontsCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(jLabel2)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(sizeText, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(lazyDogLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGap(19, 19, 19)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel1)
          .addComponent(fontsCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel2)
          .addComponent(sizeText, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addGap(18, 18, 18)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel4)
          .addComponent(colorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(colorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addGap(18, 18, 18)
        .addComponent(lazyDogLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(18, 18, 18)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addContainerGap())
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

    private void fontsComboActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_fontsComboActionPerformed
    {//GEN-HEADEREND:event_fontsComboActionPerformed
      lazyDogLabel.setFont(this.getSelectedFont());
    }//GEN-LAST:event_fontsComboActionPerformed

    private void colorButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_colorButtonActionPerformed
    {//GEN-HEADEREND:event_colorButtonActionPerformed
      colorChoosed = JColorChooser.showDialog(this, "Choose a Font Color", colorChoosed);
      if(colorChoosed == null) return;
      colorLabel.setBackground(colorChoosed);
      lazyDogLabel.setForeground(colorChoosed);
    }//GEN-LAST:event_colorButtonActionPerformed

    private void sizeTextActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_sizeTextActionPerformed
    {//GEN-HEADEREND:event_sizeTextActionPerformed
      lazyDogLabel.setFont(this.getSelectedFont());
    }//GEN-LAST:event_sizeTextActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelButtonActionPerformed
    {//GEN-HEADEREND:event_cancelButtonActionPerformed
      closed = true;
      this.close();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_saveButtonActionPerformed
    {//GEN-HEADEREND:event_saveButtonActionPerformed
      closed = false;
      this.close();
    }//GEN-LAST:event_saveButtonActionPerformed


    public void close()
    {
      this.setVisible(false);
      this.dispose();
    }

    public FontColor showFontChooser()
    {
      return FontChooser.returnFontColor(this);
    }

    public static FontColor showFontChooser(Component owner)
    {
      return FontChooser.returnFontColor(
          new FontChooser(owner));
    }

    public static FontColor returnFontColor(FontChooser fc)
    {
      if(fc == null) return null;
      if(!fc.isVisible())
        fc.setVisible(true);

      if(fc.isCanceled()) return null;

      FontColor conf = new FontColor();
      conf.setFont(fc.getSelectedFont());
      conf.setColor(fc.getSelectedColor());
      fc.close();
      return conf;
    }

    public Font getSelectedFont()
    {
      int size = 0;
      try {
        size = Integer.parseInt(sizeText.getText());
      } catch (NumberFormatException numberFormatException) {
        sizeText.setText("12");
        return getSelectedFont();
      }
      return new Font(fontsCombo.getSelectedItem().toString(),
          Font.PLAIN, size);
    }

    public Color getSelectedColor()
    {
      return colorLabel.getBackground();
    }

    public boolean isCanceled()
    {
      return closed;
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {

        /*java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FontChooser(null).setVisible(true);
            }
        });*/

      FontColor fc = FontChooser.showFontChooser(null);
      if(fc == null) System.out.println("Font Chooser Canceled!");
      else {
        System.out.println(fc.getColor().toString());
        System.out.println(fc.getFont().toString());
      }
    }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private com.power.visual.BlackButton cancelButton;
  private com.power.visual.BlackButton colorButton;
  private javax.swing.JLabel colorLabel;
  private javax.swing.JComboBox fontsCombo;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel lazyDogLabel;
  private com.power.visual.BlackButton saveButton;
  private javax.swing.JTextField sizeText;
  // End of variables declaration//GEN-END:variables

}
